package de.teamgamma.cansat.groundstation.livestream;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;

import javax.swing.ButtonGroup;

import de.szut.gui.language.Languages;
import de.szut.gui.language.TranslatableComponent;
import de.szut.gui.language.TranslatableMenu;
import de.szut.gui.options.Options;
import de.szut.gui.options.OptionsComponent;
import de.teamgamma.cansat.groundstation.control.Controller;

/**
 * Menu for multiple axes
 * @author Marc Huisinga
 *
 */
public class AxisMenu extends TranslatableMenu {

	private static final long serialVersionUID = 1390890485580931785L;
	
	private ButtonGroup axisGroup;
	private ArrayList<AxisMenuRadioButton> axisButtons;
	private ArrayList<TranslatableComponent> translatableComponents;
	private ArrayList<OptionsComponent> optionComponents;
	private LivestreamPanel panel;
	private String axisPrefix;

	/**
	 * 
	 * @param lPanel - related LivestreamPanel
	 */
	public AxisMenu(LivestreamPanel lPanel) {
		panel = lPanel;
		startAxisMenu();
	}
	
	/**
	 * 
	 * @param lPanel - related LivestreamPanel
	 * @param axisPrefix - Prefix of the axis this menu is for
	 */
	public AxisMenu(LivestreamPanel lPanel, String axisPrefix) {
		panel = lPanel;
		this.axisPrefix = axisPrefix;
		startAxisMenu();
	}
	
	private class AxisMenuRadioButtonItemListener implements ItemListener {
		@Override
		public void itemStateChanged(ItemEvent arg0) {
			if (arg0.getStateChange() == ItemEvent.SELECTED) {
				try {
					if (axisPrefix.equals("x")) {
						panel.getStream().setCurrentXAxisValue(((AxisMenuRadioButton)arg0.getSource()).getTransmissionKey());
					} else if (axisPrefix.equals("y")) {
						panel.getStream().setCurrentYAxisValue(((AxisMenuRadioButton)arg0.getSource()).getTransmissionKey());
					}
				} catch(NullPointerException e) {
					// Ignores the setting of the value
				}
			}
		}
	}
	
	private void startAxisMenu() {
		translatableComponents = new ArrayList<TranslatableComponent>();
		optionComponents = new ArrayList<OptionsComponent>();
		axisButtons = new ArrayList<AxisMenuRadioButton>();
		axisGroup = new ButtonGroup();
		Languages.prepareComponent("livestream.LivestreamPanel.menubar." + axisPrefix + "axis", this, translatableComponents);
		
		for (String currentKey : TransmittedData.ALLKEYS) {
			if (!TransmittedData.STRINGKEYS.contains(currentKey)){
				AxisMenuRadioButton currentButton = new AxisMenuRadioButton();
				currentButton.setTransmissionKey(currentKey);
				currentButton.addItemListener(new AxisMenuRadioButtonItemListener());
				axisGroup.add(currentButton);
				axisButtons.add(currentButton);
				Languages.prepareComponent("livestream.LivestreamPanel.menubar." + axisPrefix + "axis."+currentKey, currentButton, translatableComponents);
				Options.prepareComponent(axisPrefix + "axis."+currentKey, currentButton, optionComponents);
				add(currentButton);
			}
		}
		Controller.getInstance().getGuiController().getLanguages().translateListOfComponents(translatableComponents);
	}
	
	/**
	 * Gets the axis name from the relative axis key
	 * @param key - key of the axis
	 * @return name
	 */
	public String getAxisNameFromKey(String key) {
		String name = "NONE";
		for (AxisMenuRadioButton currentButton : axisButtons) {
			if (currentButton.getTransmissionKey().equals(key)) {
				name = currentButton.getText();
			}
		}
		return name;
	}
	
	/**
	 * Gets the buttons used in this AxisMenu
	 * @return buttons
	 */
	public ArrayList<AxisMenuRadioButton> getAxisButtons() {
		return axisButtons;
	}

	/**
	 * Sets the buttons used in this AxisMenu
	 * @param axisButtons
	 */
	public void setAxisButtons(ArrayList<AxisMenuRadioButton> axisButtons) {
		this.axisButtons = axisButtons;
	}

	/**
	 * Gets the prefix of this axis (e.g.: x / y)
	 * @return prefix of this axis
	 */
	public String getAxisPrefix() {
		return axisPrefix;
	}

	/**
	 * Sets the prefix of this axis (e.g.: x / y)
	 * @param axisPrefix
	 */
	public void setAxisPrefix(String axisPrefix) {
		this.axisPrefix = axisPrefix;
	}

	/**
	 * Gets the option components of the AxisMenu
	 * @return option components
	 */
	public ArrayList<OptionsComponent> getOptionComponents() {
		return optionComponents;
	}

	/**
	 * Sets the option components of the AxisMenu
	 * @param optionComponents
	 */
	public void setOptionComponents(ArrayList<OptionsComponent> optionComponents) {
		this.optionComponents = optionComponents;
	}
	
	public boolean containsSelectedButtons() {
		for (AxisMenuRadioButton button : axisButtons) {
			if (button.isSelected()) {
				return true;
			}
		}
		return false;
	}
}
